package com.cedar.demo.demo;

import org.omg.CosNaming.NameComponent;
import org.omg.DynamicAny.DynAnyFactory;
import org.omg.DynamicAny.DynAnyFactoryHelper;

import com.cedar.demo.entity.TANmsSession_IImpl;

import mtnm.tmforum.org.common.Common_IHolder;
import mtnm.tmforum.org.emsMgr.EMSMgr_I;
import mtnm.tmforum.org.emsMgr.EMSMgr_IHelper;
import mtnm.tmforum.org.emsSession.EmsSession_I;
import mtnm.tmforum.org.emsSession.EmsSession_IHolder;
import mtnm.tmforum.org.emsSession.EmsSession_IPackage.managerNames_THolder;
import mtnm.tmforum.org.emsSessionFactory.EmsSessionFactory_I;
import mtnm.tmforum.org.emsSessionFactory.EmsSessionFactory_IHelper;
import mtnm.tmforum.org.nmsSession.NmsSession_I;
import mtnm.tmforum.org.nmsSession.NmsSession_IPOA;
import mtnm.tmforum.org.notifications.EventIterator_IHolder;
import mtnm.tmforum.org.notifications.EventList_THolder;
import mtnm.tmforum.org.notifications.PerceivedSeverity_T;
/************************************************************
 Type     : client demo program code
 Description     : NCE CORBA Program development demo to get emsSession object
 ************************************************************/
public class Client {
	private Client() {
	}
	private static Client instance = new Client();
	public static Client getInstance() {
		return instance;
	}
	//ORB object
	private static org.omg.CORBA.ORB orb = null;
	//POA object reference
	private static org.omg.PortableServer.POA rootPOA = null;
	//emsSeesion object
	private static EmsSession_I emsSession = null;
	//dynamic any factory,  for translating any type to dynamic any type
	private static DynAnyFactory dynAnyFactory = null;;
	public static void log(String str) {
		System.out.println(str);
	}
	//
	public static void main(String[] args) {
		if(args.length<4){
			log("usage: java demo.Client 10.71.227.152 12001 admin test1234");
			return;
		}
		log("-------------------------------------------------------");
		log("Naming service IP: " + args[0]);
		log("Naming service port: " + args[1]);
		log("EMS user name : " + args[2]);
		log("Password for user " + args[2] + " : " + args[3]);
		log("-------------------------------------------------------");
		Client.getInstance().connect(args[0], args[1], args[2], args[3]);
	}
	/*
	 * NSIP: naming service IP
	 * NSPort: naming service port
	 * userName: an EMS user name
	 * passWord: the password for a specified EMS user
	 */
	public  int connect(String NSIP, String NSPort,String userName, String passWord) {
		try {
//initialize ORB parameters
			String argv[] = new String[2];
			argv[0] = "-ORBInitRef";
			argv[1] = "NameService=corbaloc::" + NSIP + ":" + NSPort+ "/NameService";
//The step 1: initialize ORB
			orb = org.omg.CORBA.ORB.init(argv, null);
			log("The step 1: initialize ORB successfully!");
//The step 2: get RootPOA
			rootPOA = org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
			rootPOA.the_POAManager().activate();
			log("The step 2: get RootPOA successfully!");
//the step 3: construct dynamic any factory
			dynAnyFactory = DynAnyFactoryHelper.narrow(orb
					.resolve_initial_references("DynAnyFactory"));
			log("the step 3: construct dynamic any factory."
					+ (dynAnyFactory!= null ? "successfully!" : "failed!"));
//The step 4: get naming service root object reference
			org.omg.CosNaming.NamingContextExt nc = org.omg.CosNaming.NamingContextExtHelper
					.narrow(orb.resolve_initial_references("NameService"));
			log("The step 4: get naming service root object refrence successfully!");
//The step 5: get EmsSessionFactory_I object reference from naming service
//construct EmsSessionFactory_I NameComponent,please get it's parameters from HuaWei
			org.omg.CosNaming.NameComponent[] name;
			name = new NameComponent[5];
			name[0] = new NameComponent("TMF_MTNM", "Class");
			name[1] = new NameComponent("HUAWEI", "Vendor");
			name[2] = new NameComponent("Huawei/NCE", "EmsInstance");
			name[3] = new NameComponent("2.0", "Version");
			name[4] = new NameComponent("Huawei/NCE", "EmsSessionFactory_I");
			EmsSessionFactory_I EmsSessionFactory = null;
			try {
				EmsSessionFactory = EmsSessionFactory_IHelper.narrow(nc.resolve(name));
				log("The step 5: get EmsSessionFactory_I object refrence from naming service successfully!");
			} catch (Exception ex) {
				log("The step 5: get EmsSessionFactory_I object refrence from naming service failed! \n please confirm NCE CORBA is running and EMS name!");
				ex.printStackTrace();
				return 0;
			}
//get emsSession 
			NmsSession_IPOA pNmsSessionServant = new TANmsSession_IImpl();
			NmsSession_I NmsSession =  pNmsSessionServant._this(orb);
//The step 6: invoke getEmsSession interface to login NCE CORBA
			EmsSession_IHolder emsSessionInterfaceHolder = new EmsSession_IHolder();
			try {
				EmsSessionFactory.getEmsSession(userName, passWord, NmsSession,emsSessionInterfaceHolder);
				emsSession = emsSessionInterfaceHolder.value;
				log("The step 6: invoke getEmsSession interface to login NCE CORBA successfully!");
			} catch (Exception ex) {
				log("The step 6: invoke getEmsSession interface to login NCE CORBA failed!");
				log(ex.getLocalizedMessage());
				ex.printStackTrace();
				return 0;
			}
//get and list all supported Managers
			log("The step 7: list all supported Managers:");
			managerNames_THolder managerNames_Holder = new managerNames_THolder();
			emsSession.getSupportedManagers(managerNames_Holder);
			for (int i = 0; i < managerNames_Holder.value.length; i++) {
				log("EmsManagerName[" + i + "] " + managerNames_Holder.value[i]);
			} 
// query alarm 
			Common_IHolder mgrInterface = new Common_IHolder(); 
			emsSession.getManager("EMS", mgrInterface); 
			EMSMgr_I emsmgr = EMSMgr_IHelper.narrow(mgrInterface.value); 
			String[] excludeProbCauseList = new String[0]; 
			PerceivedSeverity_T[] excludeSeverityList = new PerceivedSeverity_T[0]; 
			EventList_THolder eventList = new EventList_THolder(); 
			EventIterator_IHolder eventIt = new EventIterator_IHolder(); 
			emsmgr.getAllEMSAndMEActiveAlarms(excludeProbCauseList, excludeSeverityList, 1000, eventList, eventIt); 
			log("The step 8: query alarm successfully!");
			log("The step 9: try to connect structuredPushConsumer to the event channel.");
//get alarms from EventChannel  
			new AlarmReciever(orb,emsSession).activate();
			orb.run();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	public static void listen(){
//get alarms from EventChannel  
		new AlarmReciever(orb,emsSession).activate();
		orb.run();
	}
	/**
	 *   Getter function, return dynAnyFactory
	 */
	public static DynAnyFactory getDynAnyFactory() {
		return dynAnyFactory;
	}
	/**
	 *   Setter function, set dynAnyFactory
	 */
	public static void setDynAnyFactory(DynAnyFactory dynAnyFactory) {
		Client.dynAnyFactory = dynAnyFactory;
	}
	/**
	 *   Getter function, return emsSession
	 */
	public static EmsSession_I getEmsSession() {
		return emsSession;
	}
	/**
	 *   Setter function, set emsSession
	 */
	public static void setEmsSession(EmsSession_I emsSession) {
		Client.emsSession = emsSession;
	}
	/**
	 *   Getter function, return orb
	 */
	public static org.omg.CORBA.ORB getOrb() {
		return orb;
	}
	/**
	 *   Setter function，set orb
	 */
	public static void setOrb(org.omg.CORBA.ORB orb) {
		Client.orb = orb;
	}
	/**
	 *   Getter function, return rootPOA
	 */
	public static org.omg.PortableServer.POA getRootPOA() {
		return rootPOA;
	}
	/**
	 *   Setter function，set rootPOA
	 */
	public static void setRootPOA(org.omg.PortableServer.POA rootPOA) {
		Client.rootPOA = rootPOA;
	}
	/**
	 *   Setter function, set instance
	 */
	public static void setInstance(Client instance) {
		Client.instance = instance;
	}
}
